/**
 * Created by xuweijie on 17-5-6.
 */

//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element();
});
//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active = {
        update: function (othis) {
            layer.open({
                type: 1,
                title:'修改菜单',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '650px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: true, //开启遮罩关闭
                content: '<br><form class="layui-form" action="" method="post"><input type="text" id="menuid" hidden><div class="layui-form-item"><label class="layui-form-label">菜单名</label><div class="layui-input-block"><input id="menuname" type="text" name="menuname" required  lay-verify="required" placeholder="请输入菜单名" autocomplete="off" class="layui-input" style="width: 95%;" > </div> </div> <div class="layui-form-item"> <label class="layui-form-label">链接</label> <div class="layui-input-block"> <input id="menuurl" type="text" name="menuurl"  placeholder="请输入链接" autocomplete="off" class="layui-input" style="width: 95%;"> </div> </div> </form>',

                success: function (layero) {
                    console.log(othis);
                    if ($(othis).parents('h2').attr('title') == null) {
                        var menuInfoParent = $(othis).parents('tr');
                        var menuInfoItem = menuInfoParent.children("td");
                        $('#menuname').val(menuInfoItem[0].innerHTML);
                        $('#menuurl').val(menuInfoItem[1].innerHTML);
                        $('#menuid').val(othis.menuid);
                    } else {
                        var menuInfoParent = $(othis).parents('h2');
                        $('#menuname').val(menuInfoParent.attr('title'));
                        $('#menuid').val(othis.menuid);
                    }
                },
                yes: function (index, layero) {

                    var params = $.param({
                        menuid: $('#menuid').val(),
                        menus: $('#menuname').val(),
                        menusurl: $('#menuurl').val(),
                    },true);

                    $.ajax({
                        url: 'updatemenu.action',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            window.location.href = "/admin/menu";//重新加载该页面
                        }
                    })
                }
            });
        },
        notice: function (menu) {
            //示范一个公告层
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "delmenu.action",
                        type: 'post',
                        data: {
                            menuid: menu.menuid
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功！") {
                                window.location.href = "/admin/menu";
                            }
                            layer.msg(data);
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        }
    };
    $('.updatemenu').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.menuid = $(this).attr('menuid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.delmenu').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.menuid = $(this).attr('menuid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

//表单显示
layui.use('form', function () {
    var form = layui.form();

    //监听提交
    form.on('submit(formDemo)', function (data) {
        var menus = [];
        var menusurl = [];
        $("input:text[name=menus]").each(function(){
            menus.push($(this).val());
        });
        $("input:text[name=menusurl]").each(function(){
            menusurl.push($(this).val());
        });
        var params = $.param({
            parentId: data.field.parentId,
            menus: menus,
            menusurl: menusurl
        },true);
        $.ajax({
            url: '/addmenu',
            type: 'post',
            data: params,
            dataType: 'json',
            success: function (data) {
                window.location.href = "/admin/menu";
            }
        });
        return false;
    });
});

function addmore() {
    var parent = $('#menu-group');
    var childs = parent.find('.input-group:last');
    var item = "<div class='input-group'><br><label class='layui-form-label'>菜单名</label><div class='layui-input-block'> <input type='text' name='menus' required lay-verify='required' placeholder='请输入菜单名' autocomplete='off' class='layui-input'></div> <br> <label class='layui-form-label'>链接</label> <div class='layui-input-block'><input type='text' name='menusurl'  placeholder='请输入链接' autocomplete='off' class='layui-input'></div></div>";
    childs.after(item);
}

function removelast() {
    var parent = $('#menu-group');
    var child_group = parent.find('.input-group:last');
    if (parent.find('.input-group').length !== 1) {
        child_group.remove();
    }
}
// $(document).ready(function(){
//     /*
//      * 监听修改菜单按钮
//      * */
//     $('.updatemenu').click(function(){
//         var menuInfoParent=$(this).parents('tr');
//         var menuInfoItem=menuInfoParent.children("td");
//         $('#menuname').val(menuInfoItem[0].innerHTML);
//         $('#menuurl').val(menuInfoItem[1].innerHTML);
//         $('#menuid').val($(this).attr('menuid'));
//     });
//
//     $('.updatepmenu').click(function(){
//         var menuInfoParent=$(this).parents('h2');
//         $('#menuname').val(menuInfoParent.attr('title'));
//         $('#menuid').val($(this).attr('menuid'));
//     });
//
//     /*
//     * 提交确认
//     * */
//     $('.btconfirm').click(function () {
//         $.ajax({
//             url:'updatemenu.action',
//             type:'post',
//             data: {
//                 menuid : $('#menuid').val(),
//                 menus : $('#menuname').val(),
//                 menusurl : $('#menuurl').val(),
//             },
//             dataType:'json',
//             success:function (data) {
//                 console.log(data);
//                 window.location.href = "/admin/menu";//重新加载该页面
//             }
//         })
//     });
// });

// function ajaxMenu() {
//     $.ajax({
//         url: 'ajaxmenu',
//         type: 'post',
//         dataType: 'json',
//         success: function (data) {
//             // var objs = eval('(' + data + ')');
//             /*for (var o in objs) {
//              var jsonObj = objs[o];
//              var optionstring = "<option value=\"" + jsonObj.id + "\" >" + jsonObj.name + "</option>";
//              $("#groupname").append(optionstring);
//              }*/
//             console.log(data);
//         }
//     })
// }